package com.ruoyi.system.apiController;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.YyDoctor;
import com.ruoyi.system.service.impl.YyDoctorServiceImpl;
import com.ruoyi.system.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api/doctor")
public class YyDoctorpApiController {

    @Autowired
    private YyDoctorServiceImpl yyDoctorService;

    /**
     * 注册
     */
    @PostMapping("/add")
    public AjaxResult register(@RequestParam("account") String account,
                               @RequestParam("password") String password,
                               @RequestParam("name") String name,
                               @RequestParam("photo") MultipartFile photo,
                               @RequestParam("education") String education,
                               @RequestParam("department") String department,
                               @RequestParam("major") String major,
                               @RequestParam("gender") String gender,
                               @RequestParam("age") Integer age,
                               @RequestParam("position") String position) {
        try{
            YyDoctor doctor = new YyDoctor();
            if (photo != null){
                String photoFileName = FileUtil.savePhoto(photo);
                doctor.setPhoto(photoFileName);
            }
            // 创建医生对象
            doctor.setAccount(account);
            doctor.setPassword(password);
            doctor.setName(name);
            doctor.setEducation(education);
            doctor.setDepartment(department);
            doctor.setMajor(major);
            doctor.setGender(gender);
            //Integer转换为Long
            doctor.setAge(Long.parseLong(age.toString()));
            doctor.setPosition(position);

            // 保存医生信息到数据库
            List<YyDoctor> yyDoctors = yyDoctorService.selectYyDoctorList(doctor);
            if (yyDoctors.size() > 0){
                return AjaxResult.error("添加失败,账号已存在");
            }
            yyDoctorService.insertYyDoctor(doctor);
        }catch (IOException e){
            e.printStackTrace();
            return AjaxResult.error("医生添加失败：照片保存出错");
        }
        return AjaxResult.success("医生添加成功");
    }

    /**
     * 查询所有医生
     */
    @GetMapping("/list")
    public AjaxResult list(YyDoctor yyDoctor) {
        List<YyDoctor> yyDoctors = yyDoctorService.selectYyDoctorList(yyDoctor);
        return AjaxResult.success(yyDoctors);
    }


    /**
     * 修改
     */
    @PostMapping("/update")
    public AjaxResult update(@RequestParam(name = "doctorId", required = false) String doctorId,
                            @RequestParam(name = "account", required = false) String account,
                             @RequestParam(name = "password", required = false) String password,
                             @RequestParam(name ="name", required = false) String name,
                             @RequestParam(name ="photo", required = false) MultipartFile photo,
                             @RequestParam(name ="education", required = false) String education,
                             @RequestParam(name ="department", required = false) String department,
                             @RequestParam(name ="major", required = false) String major,
                             @RequestParam(name ="gender", required = false) String gender,
                             @RequestParam(name ="age", required = false) Integer age,
                             @RequestParam(name ="position", required = false) String position){
        try{
            YyDoctor doctor = new YyDoctor();
                if (photo != null) {
                    String photoFileName = FileUtil.savePhoto(photo);
                    doctor.setPhoto(photoFileName);
                }
            // 创建医生对象
            doctor.setAccount(account);
            doctor.setPassword(password);
            doctor.setName(name);
            doctor.setEducation(education);
            doctor.setDepartment(department);
            doctor.setMajor(major);
            doctor.setGender(gender);
            //Integer转换为Long
            doctor.setAge(Long.parseLong(age.toString()));
            doctor.setPosition(position);
            doctor.setDoctorId(Long.parseLong(doctorId));
            int i = yyDoctorService.updateYyDoctor(doctor);
            if (i < 0) {
                return AjaxResult.error("修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.success("修改成功");
    }

    @DeleteMapping("/delete/{doctorId}")
    public AjaxResult delete(@PathVariable("doctorId") Long doctorId) {
        int i = yyDoctorService.deleteYyDoctorByDoctorId(doctorId);
        if (i < 0) {
            return AjaxResult.error("删除失败");
        }
        return AjaxResult.success("删除成功");
    }

}
